<?php
class WidgetModel extends Model {
	protected	$tableName	=	'portal_widget';
	protected	$fields	=	array (0 =>'widgetId',1 =>'uid',2 =>'pluginId',3 =>'pageId',4 =>'channelId',5 =>'tagLib',6=>'content',7=>'ext',8=>'cache',9=>'cTime',10=>'mTime',11=>'cacheTime','_autoinc' =>true,'_pk' =>'widgetId');

	public function checkCache($sign){
		//得到该widget的缓存配置消息
		$map['pluginId'] = $sign;
		$cacheInfo = $this->where($map)->field('cacheTime,cTime,mTime,cache')->find();
		if(!$cacheInfo) return true;
		if(empty($cacheInfo['cache'])) return true;
		//如果等于0.则为每次都要渲染
		if($cacheInfo['cacheTime'] == 0){
			return true;
		}else{
			//有缓存时间
			$time = $cacheInfo['cacheTime'] * 60;
			if(time()-$cacheInfo['mTime']>=$time){
				return true;
			}else{
				return false;
			}
		}
	}

	public function hasCustomStyle($sign){
		$map['pluginId'] = $sign;
		return $this->where($map)->count()?true:false;
	}

	public function getAttr($sign){
		$map['pluginId'] = $sign;
		$ext = unserialize($this->where($map)->getField('ext'));
		return unserialize($ext['attr']);
	}

	public function getTagInfo($sign){
		$map['pluginId'] = $sign;
		$data = $this->where($map)->field('pluginId,tagLib,content,ext,status')->find();
		$ext = unserialize($data['ext']);
		$result['tagName'] = $ext['tagInfo']['name'];
		$result['content'] = $data['content'];
		$result['tagLib']  = $data['tagLib'];
		$result['attr'] = unserialize($ext['attr']);
		return $result;
	}

	public function getTagInofs(array $sign){
		$map['pluginId'] = array('in',$sign);
		$data = $this->where($map)->field('pluginId,tagLib,content,ext,status')->findAll();
		$result = array();
		foreach ($data as $value) {
			$ext = unserialize($value['ext']);
			$result[$value['pluginId']]['tagName'] = $ext['tagInfo']['name'];
			$result[$value['pluginId']]['content'] = $value['content'];
			$result[$value['pluginId']]['tagLib']  = $value['tagLib'];
			$result[$value['pluginId']]['attr'] = unserialize($ext['attr']);
		}
		return $result;
	}

	/**
	 * 通过id获取到该widget的tagName
	 */
	public function getTagName($widgetId){
		$map['widgetId'] = $widgetId;
		$ext = unserialize($this->where($map)->getField('ext'));
		return $ext['tagInfo']['name'];
	}

	/**
	 * 通过id获取到该widget的模板
	 */

	public function getTemplateByPluginId($pluginId){
		$map['pluginId'] = $pluginId;
		$content = $this->where($map)->getField('content');
		return empty($content)?false:$content;
	}

	/**
	 * 通过id获取到该widget的模板
	 */
	public function getTemplateByWidgetId($WidgetId){
		$map['widgetId'] = $WidgetId;
		$content = $this->where($map)->getField('content');
		return empty($content)?false:$content;
	}
	/**
	 * 通过Id更新该widget的样式模板
	 */
	public function setTemplate($widgetId,$content) {
		$map['widgetId'] = $widgetId;
		$save['content'] = $content;
		return $this->where($map)->save($save);
	}


	public function getCache($sign){
		$map['pluginId'] = $sign;
		return $this->where($map)->getField('cache');
	}

	public function saveCache($sign,$content){
		$map['pluginId'] = $sign;
		$save['cache'] = $content;
		$save['mTime'] = time();
		$result = $this->where($map)->save($save);
	}

	public function checkHasWidget($sign){
		$map['pluginId'] = $sign;
		$count = $this->where($map)->count();
		return $count>0?true:false;
	}
	public function updateWidget($sign,$attr,$value,$tagInfo){
		//取出属性。
		$map['pluginId'] = $sign;
		$ext = $this->where($map)->getField('ext');
		$attr = unserialize($ext['attr']);

		//对比属性值。如果相等，不做处理。如果不等。update操作
		if($attr === $attr)
		return true;
		$ext['attr'] = serialize($attr);
		$save['ext'] = $ext;
		$this->where($map)->save($save);
	}
}
?>